package com.anan.builder;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class BuildAnnotation {

    public static void create(String projectName){

        String path = System.getProperty("user.dir")+ "/" + projectName + "/src/main/java/com/anan/annotation";
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("创建 " + path + " 目录失败");
            }
        }
        createCheckObject(path);
        createCheckValue(path);
        createGlobalInterceptor(path);
    }

    private static void createCheckObject(String path) {

        File beanFile = new File(path, "CheckObject.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.annotation;\n" +
                    "\n" +
                    "import java.lang.annotation.ElementType;\n" +
                    "import java.lang.annotation.Retention;\n" +
                    "import java.lang.annotation.RetentionPolicy;\n" +
                    "import java.lang.annotation.Target;\n" +
                    "\n" +
                    "@Retention(RetentionPolicy.RUNTIME)\n" +
                    "@Target({ElementType.PARAMETER, ElementType.FIELD})\n" +
                    "public @interface CheckObject {\n" +
                    "\n" +
                    "\n" +
                    "    boolean required() default false;  // 是否必须\n" +
                    "\n" +
                    "    String[] checkProperties() default {\"all\"};  // 校验属性 默认校验全部属性\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    private static void createCheckValue(String path) {

        File beanFile = new File(path, "CheckValue.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.annotation;\n" +
                    "\n" +
                    "import java.lang.annotation.ElementType;\n" +
                    "import java.lang.annotation.Retention;\n" +
                    "import java.lang.annotation.RetentionPolicy;\n" +
                    "import java.lang.annotation.Target;\n" +
                    "\n" +
                    "@Retention(RetentionPolicy.RUNTIME)\n" +
                    "@Target({ElementType.PARAMETER, ElementType.FIELD})\n" +
                    "public @interface CheckValue {\n" +
                    "\n" +
                    "    /**\n" +
                    "     * 最小长度\n" +
                    "     *\n" +
                    "     * @return\n" +
                    "     */\n" +
                    "    int min() default -1;\n" +
                    "\n" +
                    "    /**\n" +
                    "     * 最大长度\n" +
                    "     *\n" +
                    "     * @return\n" +
                    "     */\n" +
                    "    int max() default -1;\n" +
                    "\n" +
                    "    boolean required() default false;  // 是否必须\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    private static void createGlobalInterceptor(String path) {

        File beanFile = new File(path, "GlobalInterceptor.java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;

        try {
            out = new FileOutputStream(beanFile);
            outw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            bw = new BufferedWriter(outw);
            bw.write("package com.anan.annotation;\n" +
                    "\n" +
                    "import org.springframework.web.bind.annotation.Mapping;\n" +
                    "\n" +
                    "import java.lang.annotation.*;\n" +
                    "\n" +
                    "@Target({ElementType.METHOD, ElementType.TYPE})  // 可以在方法 和 类 使用\n" +
                    "@Retention(RetentionPolicy.RUNTIME)  // 运行时\n" +
                    "@Documented  // Java注释标记，用于指示该注解将被包含在Java文档中  @Documented是一个Java注解，用于指示该注解应该被包含在生成的Java文档中。当开发人员在编写注解时使用@Documented标记时，它会告诉文档生成工具将该注解的信息包含在Java文档中。这对于阅读文档的其他开发人员很有用，因为他们可以在文档中看到该注解的用途和作用。\n" +
                    "@Mapping  // @Mapping是一个Spring框架的注解，用于将HTTP请求映射到控制器方法上。它可以用于注解@Controller类中的方法，使得当一个特定的HTTP请求到达应用程序时，能够调用相应的控制器方法进行处理。@Mapping注解可以指定HTTP请求的方法（GET、POST、PUT、DELETE等）、请求的URL路径、请求的参数、请求头等信息，以及请求的响应类型等。使用@Mapping注解可以使代码更加简洁、易于维护，并且能够更好地组织应用程序的请求处理逻辑。\n" +
                    "public @interface GlobalInterceptor {  // 全局拦截器注解  AOP切面\n" +
                    "\n" +
                    "    /**\n" +
                    "     * 校验登录\n" +
                    "     *\n" +
                    "     * @return\n" +
                    "     */\n" +
                    "    boolean checkLogin() default false;\n" +
                    "\n" +
                    "    /**\n" +
                    "     * 校验参数\n" +
                    "     *\n" +
                    "     * @return\n" +
                    "     */\n" +
                    "    boolean checkParams() default false;\n" +
                    "\n" +
                    "    /**\n" +
                    "     * 校验管理员\n" +
                    "     *\n" +
                    "     * @return\n" +
                    "     */\n" +
                    "    boolean checkAdmin() default false;\n" +
                    "}\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();

                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
